package sort;

import java.util.Arrays;

public class Heap {
    public static void main(String[] args) {
        logarithm(100000, 10, 10);
        runTime(1, 100000, 100000);
    }

    /**
     * 堆排序算法， 平均O(n log n), 最好O(n log n), 最坏O(n log n) 空间复杂度O(1) in-place 不稳定
     *
     * @param array
     * @return
     */
    public static int[] heap(int[] array) {
        for (int i = array.length / 2 - 1; i >= 0; i--) {
            adjustHeap(array, i, array.length);
        }
        for (int j = array.length - 1; j > 0; j--) {
            int temp = array[j];
            array[j] = array[0];
            array[0] = temp;
            adjustHeap(array, 0, j);
        }
        return array;
    }

    /**
     * 构建大顶堆
     *
     * @param array
     * @param i
     * @param length
     */
    public static void adjustHeap(int[] array, int i, int length) {
        int temp = array[i];
        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if (k + 1 < length && array[k] < array[k + 1]) {
                k++;
            }
            if (array[k] > temp) {
                array[i] = array[k];
                i = k;
            } else {
                break;
            }
        }
        array[i] = temp;
    }

    /**
     * 打印数组
     *
     * @param array
     */
    public static void show(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    /**
     * 产生随机数组
     *
     * @param length
     * @return
     */
    public static int[] getRandomArray(int length, int numRange) {
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = (int) (Math.random() * numRange);
        }
        return array;
    }

    /**
     * 对数器
     *
     * @param testNum
     * @param arrayLen
     * @param numRange
     */
    public static void logarithm(int testNum, int arrayLen, int numRange) {
        boolean runResult = true;
        for (int i = 0; i < testNum; i++) {
            int[] randomArray = getRandomArray(arrayLen, numRange);
            boolean result = logarithmRun(randomArray);
            if (!result) {
                runResult = false;
            }
        }
        if (runResult) {
            System.out.println(true);
        }
    }

    /**
     * 对数器计算过程
     *
     * @param array
     * @return
     */
    public static boolean logarithmRun(int[] array) {
        //复制传入样本
        int[] newArray = new int[array.length];
        for (int i = 0; i < newArray.length; i++) {
            newArray[i] = array[i];
        }
        //获取实际算法输出
        int[] actualVal = heap(array);
        //获取期望输出
        Arrays.sort(newArray);
        int[] expectedVal = newArray;
        //核对结果
        boolean equals = isEquals(actualVal, expectedVal);
        if (equals) {
            return true;
        } else {
            System.out.println("expectedVal:");
            show(expectedVal);
            System.out.println("actualVal:");
            show(actualVal);
            return false;
        }
    }

    /**
     * 结果核对
     *
     * @param actualVal
     * @param expectedVal
     * @return
     */
    public static boolean isEquals(int[] actualVal, int[] expectedVal) {
        if (actualVal.length != expectedVal.length) {
            return false;
        }
        if (actualVal != null && expectedVal == null || actualVal == null && expectedVal != null) {
            return false;
        }
        for (int i = 0; i < actualVal.length; i++) {
            if (actualVal[i] != expectedVal[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 算法运行时间
     *
     * @param testNum
     * @param arrayLen
     * @param numRange
     */
    public static void runTime(int testNum, int arrayLen, int numRange) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < testNum; i++) {
            int[] randomArray = getRandomArray(arrayLen, numRange);
            heap(randomArray);
        }
        long endTime = System.currentTimeMillis();
        long consumingTime = endTime - startTime;
        System.out.println("startTime:" + startTime + "\nendTime:" + endTime + "\nconsumingTime:" + consumingTime);
    }
}
